package es.udc.cameraar2;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;

import com.google.zxing.Result;
import com.google.zxing.ResultPoint;


/*
 * Class:  Panel.java
 * Function: Overlay to CameraPreview
 *     All functions related to the overlay.
 * 
 */

public class Panel extends SurfaceView implements SurfaceHolder.Callback{
			
		Context c;	    	    	
		 
		private String text;			
		
		public int getMinx() {
			return minx;
		}



		public void setMinx(int minx) {
			this.minx = minx;
		}

		public int getMaxx() {
			return maxx;
		}

		public void setMaxx(int maxx) {
			this.maxx = maxx;
		}

		public int getMiny() {
			return miny;
		}

		public void setMiny(int miny) {
			this.miny = miny;
		}

		public int getMaxy() {
			return maxy;
		}

		public void setMaxy(int maxy) {
			this.maxy = maxy;
		}

		
		private CameraPreview preview = null;	
		
		Paint paint = new Paint();
		
		int minx=0,maxx=0,miny=0,maxy=0;
		
		ResultPoint[] points = new ResultPoint[]{new ResultPoint(0,0)};
		
	    public Panel(Context context) {		    
			super(context);				
			getHolder().addCallback(this);		
			
			setZOrderOnTop(true);
			getHolder().setFormat(PixelFormat.TRANSLUCENT);								
						
			
			text = "notfound";			
			c = context;		
	    }

	   

	    public void surfaceCreated(SurfaceHolder holder) {
			// TODO Auto-generated method stub	    	
		}
		

		public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2,
				int arg3) {
			// TODO Auto-generated method stub
		}		
		
		
		public void surfaceDestroyed(SurfaceHolder holder) {					
		}
		
		
		/*
		 * Touch event: to control each touch
		 * 
		 * @see android.view.View#onTouchEvent(android.view.MotionEvent)
		 */
		@Override
		public synchronized boolean onTouchEvent(MotionEvent event) {
	        
			Context context = getContext();
			String msg;
			int duration = 50;
    		Toast toast;    		    	
    		
			switch(event.getAction()){
	        case MotionEvent.ACTION_CANCEL:	        	
        		msg = "Cancel...";        		
        		toast = Toast.makeText(context, msg, duration);
        		toast.show(); break;
        		        		
        	//Cuando pinchamos recogemos el lugar donde lo hacemos	
	        case MotionEvent.ACTION_DOWN:		        	
	        	        	
        		break;
        		
        	//Cuando soltamos analizamos que hacer	
	        case MotionEvent.ACTION_UP:	        	
	        
	        	msg = "Press...";        		
        		toast = Toast.makeText(context, msg, duration);
        		toast.show();
        		
        		if(preview!=null){        			
        			this.text = "notFound";
        		}
        		
	        
	        	break;
	        case MotionEvent.ACTION_MOVE: 	        	
	        	//SCROLL
	        	/*//TODO: FALTA MIRAR ESTO
	        	
	        	if((Robot.getRobot().getIndicePos()!=-1) && (mandos)){
	        		if(coordxpus<=anchoMandos){
	        			if(coordypus>canvasHeight/2){
	        			}
	        			//else
	        			
	        		}
	        		else if(coordxpus>=canvasWidth-anchoMandos){
	        			if(coordypus>canvasHeight/2){
	        			}
	//        			else 
	        		}
	        		else if(event.getDownTime()>1000){
		        		coordxsolt = event.getX();
			        	coordysolt = event.getY();
			        	coordxVista = coordxVista + (coordxsolt-coordxpus);
		        		coordyVista = coordyVista + (coordysolt-coordypus);
		        		coordxpus = event.getX();
			        	coordypus = event.getY();			        	
		        	}
	        	}	        		        		        
	        	else if(event.getDownTime()>1000){
	        		coordxsolt = event.getX();
		        	coordysolt = event.getY();
		        	coordxVista = coordxVista + (coordxsolt-coordxpus);
	        		coordyVista = coordyVista + (coordysolt-coordypus);
	        		coordxpus = event.getX();
		        	coordypus = event.getY();		        	
	        	}	        
	        	*/break;
	        }
	        event.getX();
	        event.getY();
	        //invalidate();
	        // TODO Auto-generated method stub
	        return true;
	    }
		
	
		/*
		 * To paint everything in Canvas
		 */
	
		public void onDraw(Canvas can,Result result) {											
			
			paint.setAntiAlias(false);		
	        paint.setStyle(Paint.Style.STROKE); 
	        paint.setTextSize(20);
	        paint.setColor(Color.BLACK); 	        
	        can.drawText("Test Text: "+getText(), 50, 50, paint);	       
	        //Log.d("PANEL","#############    PINTOOOO -"+getText()+"     ###########");	 
	        
	        pintarBarra(can);
	        	        	        
	        pintarPuntos(can,result);
	        
	        pintarFrame(can);
	        
	        pintarInfo(can);
		}

		private void pintarInfo(Canvas can) {
			Paint info = new Paint(); 
        	info.setStyle(Paint.Style.FILL_AND_STROKE);
        	info.setStrokeWidth(2);
        	info.setTextSize(17);
        	info.setColor(Color.GREEN);
	        can.drawText("TimeDraw: ", can.getHeight()-100, 16 , info);
			
		}



		private void pintarFrame(Canvas can) {			
			
			paint.setStyle(Paint.Style.STROKE); 
	        paint.setTextSize(20);
	        paint.setColor(Color.YELLOW);
	        
	        Path path = new Path();
	        path.moveTo(minx, miny);
	        //can.drawText(""+minx+","+miny, minx, miny, paint);
	        path.lineTo(minx, maxy);
	        path.moveTo(minx, maxy);
	        //can.drawText(""+minx+","+maxy, minx, maxy, paint);
	        path.lineTo(maxx, maxy);
	        path.moveTo(maxx, maxy);
	        //can.drawText(""+maxx+","+maxy, maxx, maxy, paint);
	        path.lineTo(maxx, miny);
	        path.moveTo(maxx, miny);
	        //can.drawText(""+maxx+","+miny, maxx, miny, paint);
	        path.lineTo(minx, miny);
	        path.moveTo(minx, miny);
			can.drawPath(path, paint);
										        	     
		}



		private void pintarPuntos(Canvas can, Result result) {
			
			paint.setStyle(Paint.Style.STROKE); 
	        paint.setTextSize(20);
	        paint.setColor(Color.YELLOW);
			
	        if(result!=null)
	        	points = result.getResultPoints();
	        
	        
	        /*Points: 0- down-left
	         *        1- up-left
	         *        2- up-right
	         *        3- down right
	         */
	        
	        int i=0;	       
			for(ResultPoint point:points){
				
				can.drawText("+"+i, point.getX(), point.getY(), paint);
				i++;
			}			 	        	        
			
		}



		private void pintarBarra(Canvas can) {
			// TODO Auto-generated method stub
			//Pintamos los mandos
        	Rect mandoizq = new Rect();        	
        	
        	mandoizq.set(0, 0, 28, 24);
        	
        	Paint mandosp = new Paint();
        	mandosp.setColor(Color.DKGRAY);
        	mandosp.setStyle(Style.FILL);        	            
        	mandosp.setAntiAlias(false);
        	can.drawRect(mandoizq, mandosp);            	                
        	
        	Paint qrletter = new Paint(); 
        	qrletter.setStyle(Paint.Style.FILL_AND_STROKE);
        	qrletter.setStrokeWidth(2);
        	qrletter.setTextSize(17);
        	qrletter.setColor(Color.BLACK);
	        can.drawText("QR", 1, 16 , qrletter);
        	        	       
		}



		String getText() {
			return text;
		}

		void setText(String text) {
			Log.d("PANEL","SET TEXT TO "+text);
			this.text = text;
		}

		CameraPreview getPreview() {
			return preview;
		}

		void setPreview(CameraPreview preview) {
			this.preview = preview;
		}							
}

